package cn.talkimages.web.admin.actions.pic;

import java.io.File;
import java.util.List;

import cn.talkimages.framework.utils.string.StringUtil;
import cn.talkimages.web.admin.constant.AdminConstDef;
import cn.talkimages.web.beans.AdminBean;
import cn.talkimages.web.beans.BrandBean;
import cn.talkimages.web.beans.ImageBean;
import cn.talkimages.web.beans.PhotographerBean;

public class PictureUpdateAction extends PictureAction {

	private static final long serialVersionUID = -7333455161969055487L;

	private String updateType;

	private List<BrandBean> brands;

	private List<PhotographerBean> photographers;

	// ----------------------- Form Properties ---------------------- //

	/** 编号 */
	private String id;

	/** 图片中文标题 */
	private String titleZh;

	/** 图片英文标题 */
	private String titleEn;

	/** 商标ID */
	private String brandId;

	/** 摄影师ID */
	private String phogId;

	/** 东方图片 */
	private String orient;

	/** 授权许可类型 */
	private String license;

	/** 肖像权 */
	private String portrait;

	/** 物权 */
	private String property;

	/** 限制信息 */
	private String stint;

	/** 图片尺寸 */
	private String size;

	/** 图片价格 */
	private String price;

	/** 全景图片 */
	private String panorama;

	/** 图片颜色类型 */
	private String pcolor;

	/** 图片规格 */
	private String pnorm;

	/** 创意图片 */
	private String creative;

	/** 版权所有 */
	private String copyright;

	private File imgRelData;

	private String imgRelDataFileName;

	// ---------------------------- 更新 ---------------------------- //

	/**
	 * 图片 新建/更新
	 * 
	 * @throws Exception
	 */
	public String update() throws Exception {

		String result = ERROR;

		init();

		if (StringUtil.isBlank(id)) {
			updateType = AdminConstDef.UpdateType.Add;

			result = add();

		} else {
			updateType = AdminConstDef.UpdateType.Edit;

			result = edit();
		}

		return result;
	}

	// ---------------------------- 创建 ---------------------------- //

	/**
	 * 新建图片
	 * 
	 * @throws Exception
	 */
	public String add() throws Exception {

		return SUCCESS;
	}

	// ---------------------------- 编辑 ---------------------------- //

	/**
	 * 编辑图片
	 * 
	 * @throws Exception
	 */
	public String edit() throws Exception {

		if (!StringUtil.isNumeric(id)) {
			return NOTFOUND;
		}

		// KeywordBean keyword =
		// keywordService.getKeywordById(Long.parseLong(id));

		// unpackageData(keyword);

		return SUCCESS;
	}

	// ---------------------------- 提交 ---------------------------- //

	/**
	 * 保存图片
	 * 
	 * @throws Exception
	 */
	public String save() throws Exception {

		init();

		// 检查入力数据有效性
		if (!validateInputData()) {
			return INPUT;
		}

		// gather data from form
		ImageBean imageBean = packageData();

		if (AdminConstDef.UpdateType.Add.equals(updateType)) {
			getImageService().insertImages(imageBean, imgRelData);
		} else {
			// keywordService.updateKeywordById(keywordBean);
		}

		return SUCCESS;
	}

	/**
	 * 检查入力数据有效性
	 * 
	 * @return boolean 有效:true 有错误数据:false
	 */
	protected boolean validateInputData() {

		if (null == imgRelData) {
			addActionError("请上传图片关联文件。");
		} else if (!StringUtil.endsWith(imgRelDataFileName, ".xls")
				&& !StringUtil.endsWith(imgRelDataFileName, ".xlsx")) {
			addActionError("图片关联文件类型不正确。");
		}

		if (!StringUtil.isBlank(brandId) && !StringUtil.isNumeric(brandId)) {
			addActionError("品牌编号不正确。");
		}

		if (!StringUtil.isBlank(phogId) && !StringUtil.isNumeric(phogId)) {
			addActionError("摄影师编号不正确。");
		}

		return getActionErrors().size() == 0;
	}

	/**
	 * 收集表单数据
	 * 
	 * @return KnowledgeBean
	 */
	protected ImageBean packageData() {

		AdminBean loginUser = getLoginUser();
		ImageBean imageBean = new ImageBean();

		if (StringUtil.isNumeric(id))
			imageBean.setId(Long.parseLong(id));

		imageBean.setTitleZh(titleZh);
		imageBean.setTitleEn(titleEn);
		imageBean.setBrandId(Long.parseLong(brandId));
		imageBean.setPhogId(Long.parseLong(phogId));
		imageBean.setOrient(orient);
		imageBean.setLicense(license);
		imageBean.setPortrait(portrait);
		imageBean.setProperty(property);
		imageBean.setStint(stint);
		imageBean.setSize(size);
		imageBean.setPrice(price);
		imageBean.setPanorama(panorama);
		imageBean.setPcolor(pcolor);
		imageBean.setPnorm(pnorm);
		imageBean.setCreative(creative);
		imageBean.setCopyright(copyright);
		imageBean.setCreateId(loginUser.getLoginId());
		imageBean.setUpdateId(loginUser.getLoginId());

		return imageBean;
	}

	/**
	 * 填充表单数据
	 */
	protected void unpackageData(ImageBean imageBean) {

		id = String.valueOf(imageBean.getId());

		titleZh = imageBean.getTitleZh();
		titleEn = imageBean.getTitleEn();
		brandId = imageBean.getBrandId() == null ? null : String
				.valueOf(imageBean.getBrandId());
		phogId = imageBean.getPhogId() == null ? null : String
				.valueOf(imageBean.getPhogId());
		orient = imageBean.getOrient();
		license = imageBean.getLicense();
		portrait = imageBean.getPortrait();
		property = imageBean.getProperty();
		stint = imageBean.getStint();
		size = imageBean.getSize();
		price = imageBean.getPrice();
		panorama = imageBean.getPanorama();
		pcolor = imageBean.getPcolor();
		pnorm = imageBean.getPnorm();
		creative = imageBean.getCreative();
		copyright = imageBean.getCopyright();

	};

	// ---------------------------- 删除 ---------------------------- //

	/**
	 * 删除关键字
	 * 
	 * @throws Exception
	 */
	protected String delete() throws Exception {

		updateType = AdminConstDef.UpdateType.Delete;

		return SUCCESS;
	}

	// ----------------------- Commons ----------------------- //

	/**
	 * 初始化页面中需要的基础数据
	 * 
	 * @throws Exception
	 */
	protected void init() throws Exception {

		// 取得所有的品牌
		brands = getBrandService().getBrands();

		// 如果存在品牌信息 取得该品牌下的摄影师
		if (!StringUtil.isBlank(brandId)) {
			photographers = getPhotographerService().getPhotographersByBrandId(
					Long.parseLong(brandId));
		}

	}

	// ----------------------- Getter & Setter ----------------------- //

	public String getUpdateType() {
		return updateType;
	}

	public void setUpdateType(String updateType) {
		this.updateType = updateType;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitleZh() {
		return titleZh;
	}

	public void setTitleZh(String titleZh) {
		this.titleZh = titleZh;
	}

	public String getTitleEn() {
		return titleEn;
	}

	public void setTitleEn(String titleEn) {
		this.titleEn = titleEn;
	}

	public String getBrandId() {
		return brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public String getPhogId() {
		return phogId;
	}

	public void setPhogId(String phogId) {
		this.phogId = phogId;
	}

	public String getOrient() {
		return orient;
	}

	public void setOrient(String orient) {
		this.orient = orient;
	}

	public String getLicense() {
		return license;
	}

	public void setLicense(String license) {
		this.license = license;
	}

	public String getPortrait() {
		return portrait;
	}

	public void setPortrait(String portrait) {
		this.portrait = portrait;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getStint() {
		return stint;
	}

	public void setStint(String stint) {
		this.stint = stint;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getPanorama() {
		return panorama;
	}

	public void setPanorama(String panorama) {
		this.panorama = panorama;
	}

	public String getPcolor() {
		return pcolor;
	}

	public void setPcolor(String pcolor) {
		this.pcolor = pcolor;
	}

	public String getPnorm() {
		return pnorm;
	}

	public void setPnorm(String pnorm) {
		this.pnorm = pnorm;
	}

	public String getCreative() {
		return creative;
	}

	public void setCreative(String creative) {
		this.creative = creative;
	}

	public String getCopyright() {
		return copyright;
	}

	public void setCopyright(String copyright) {
		this.copyright = copyright;
	}

	public File getImgRelData() {
		return imgRelData;
	}

	public void setImgRelData(File imgRelData) {
		this.imgRelData = imgRelData;
	}

	public List<BrandBean> getBrands() {
		return brands;
	}

	public void setBrands(List<BrandBean> brands) {
		this.brands = brands;
	}

	public List<PhotographerBean> getPhotographers() {
		return photographers;
	}

	public void setPhotographers(List<PhotographerBean> photographers) {
		this.photographers = photographers;
	}

	public String getImgRelDataFileName() {
		return imgRelDataFileName;
	}

	public void setImgRelDataFileName(String imgRelDataFileName) {
		this.imgRelDataFileName = imgRelDataFileName;
	}

}
